package com.qubuyer.business.auth.view

import android.os.Bundle
import android.text.TextUtils
import android.view.View
import android.view.ViewGroup
import androidx.core.view.isVisible
import androidx.core.widget.doAfterTextChanged
import com.blankj.utilcode.util.ToastUtils
import com.qubuyer.R
import com.qubuyer.business.auth.viewmodel.SetPwdViewModel
import com.qubuyer.core.app.MVVMActivity
import com.qubuyer.databinding.ActivitySetPwdBinding
import com.qubuyer.utils.StatusBarUtil

class SetPwdActivity : MVVMActivity<ActivitySetPwdBinding, SetPwdViewModel>() {
    private var mToken: String? = null

    override val viewModel: SetPwdViewModel by injectVMByActivity()

    override fun onDataBind(dataBinding: ActivitySetPwdBinding) {
        dataBinding.host = this
    }

    override val layoutId: Int
        get() = R.layout.activity_set_pwd

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val layoutParams = dataBinding.ivBack.layoutParams as ViewGroup.MarginLayoutParams
        layoutParams.topMargin += StatusBarUtil.getStatusBarHeight()
        dataBinding.ivBack.layoutParams = layoutParams
        StatusBarUtil.setStatusBarLightMode(this, true)
        mToken = intent.getStringExtra("token")

        dataBinding.etPwd.doAfterTextChanged {
            dataBinding.ivDelPwd.isVisible = !it.isNullOrEmpty()
            dataBinding.tvLogin.isEnabled = dataBinding.etPwd.length() > 0 && dataBinding.etPwdSecond.length() > 0 && dataBinding.etInviteCode.length() > 0
        }
        dataBinding.etPwdSecond.doAfterTextChanged {
            dataBinding.ivDelPwdSecond.isVisible = !it.isNullOrEmpty()
            dataBinding.tvLogin.isEnabled = dataBinding.etPwd.length() > 0 && dataBinding.etPwdSecond.length() > 0 && dataBinding.etInviteCode.length() > 0
        }
        dataBinding.etInviteCode.doAfterTextChanged {
            dataBinding.ivDelInviteCode.isVisible = !it.isNullOrEmpty()
            dataBinding.tvLogin.isEnabled = dataBinding.etPwd.length() > 0 && dataBinding.etPwdSecond.length() > 0 && dataBinding.etInviteCode.length() > 0
        }

        viewModel.thirdPartLoginResult.observe(this, {
            when (it) {
                200 -> finish()
            }
        })
    }

    fun onClick(v: View) {
        when (v.id) {
            R.id.ivBack -> finish()
            R.id.ivDelPwd -> dataBinding.etPwd.setText("")
            R.id.ivDelPwdSecond -> dataBinding.etPwdSecond.setText("")
            R.id.ivDelInviteCode -> dataBinding.etInviteCode.setText("")
            R.id.tvLogin -> {
                val pwd = dataBinding.etPwd.text.toString()
                val secondPwd = dataBinding.etPwdSecond.text.toString()
                val inviteCode = dataBinding.etInviteCode.text.toString()
                if (TextUtils.isEmpty(pwd)) {
                    ToastUtils.showShort("请输入密码")
                    return
                }
                if (TextUtils.isEmpty(secondPwd)) {
                    ToastUtils.showShort("请确认密码")
                    return
                }
                if (TextUtils.isEmpty(inviteCode)) {
                    ToastUtils.showShort("请输入邀请码")
                    return
                }
                if (!pwd.equals(secondPwd, ignoreCase = true)) {
                    ToastUtils.showShort("两次密码输入不一致, 请重新输入")
                    return
                }
                viewModel.setPwd(mToken, inviteCode, pwd, secondPwd)
            }
        }
    }
}